import {
  myRequesy
} from '../../request/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 选项卡 
    tablist: [{
        name: "关注",
        status: true,
        id: 0
      },
      {
        name: "粉丝",
        status: false,
        id: 1
      }
    ],
    watchlist: [], //关注列表
    fanslist: [], // 粉丝列表

    Hibom: false, //控制粉丝列表底部弹窗事件
    follatt: false, //控制关注列表底部弹窗事件
    username: '',
    follpageNum: 1, //关注页码值
    fanspageNum: 1, //粉丝页码值
    listId: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (options.status) {
      this.getVermicelli()
      let tablist = this.data.tablist
      tablist.forEach(item => item.status = !item.status)
      this.setData({
        tablist
      })
    } else {
      this.getAttentionList()
    }


  },
  // 顶部关注/粉丝切换回调
  btnTab(e) {
    this.setData({
      follpageNum: 1,
      fanspageNum: 1,
    })
    let index = e.currentTarget.dataset.index
    let tablist = this.data.tablist
    tablist.map((item, i) => i === index ? item.status = true : item.status = false)
    this.setData({
      tablist
    })
    if (index === 1) {
      this.getVermicelli()
    } else {
      this.getAttentionList()
    }
  },


  // 粉丝列表长按显示底部弹窗触发的回调
  handleLongPress(e) {
    console.log(e);
    this.setData({
      Hibom: true,
      removeId: e.currentTarget.dataset.id,
      removeIndex: e.currentTarget.dataset.index
    })


  },
  // 关闭粉丝列表底部弹窗
  btnRome() {
    this.setData({
      Hibom: false,
      follatt: false
    })
  },

  // 关注列表 粉丝 底部弹窗回调
  attbtnTap(e) {
    let that = this
    that.setData({
      activeIndex: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.state !== 0) {
      that.setData({
        follatt: true,
        username: e.currentTarget.dataset.item,
        userId: e.currentTarget.dataset.id
      })
    } else {
      myRequesy({
        url: 'follow/add',
        method: 'POST',
        data: {
          user_id: e.currentTarget.dataset.id
        }
      }).then(res => {
        if (res.data.code === 0) {
          wx.showToast({
            title: '已关注',
          })
          let {
            fanslist,
            listId
          } = that.data
          fanslist[e.currentTarget.dataset.index].follow_state = 2
          that.setData({
            fanslist
          })

          let obj = {
            id: fanslist[e.currentTarget.dataset.index].user.id,
            follow_state: fanslist[e.currentTarget.dataset.index].follow_state
          }
          if (listId.find(item => item.id == obj.id) !== undefined) {
            listId.splice(listId.findIndex(item1 => item1.id == obj.id), 1)
          }
          listId.push(obj)
          that.setData({
            listId
          })
          wx.setStorageSync('listId', listId)
        }
      })
    }

  },

  //取消关注用户
  headleConfirm() {
    let that = this
    let {
      watchlist,
      activeIndex,
      listId,
      fanslist,
      tablist
    } = that.data
    myRequesy({
      url: 'follow/take-off',
      method: 'POST',
      data: {
        user_id: that.data.userId
      }
    }).then(res => {
      if (res.data.code === 0) {
        wx.showToast({
          title: '已取消',
        })

        let obj = {
          id: tablist[0].status ? watchlist[activeIndex].user.id : fanslist[activeIndex].user.id,
          follow_state: 0
        }
        if (listId.find(item => item.id == obj.id) !== undefined) {
          listId.splice(listId.findIndex(item1 => item1.id == obj.id), 1)
        }
        listId.push(obj)
        that.setData({
          listId
        })
        wx.setStorageSync('listId', listId)
        if (tablist[0].status) {
          watchlist.splice(activeIndex, 1)
        } else {
          fanslist[activeIndex].follow_state = 0
        }

        that.setData({
          follatt: false,
          follpageNum: 1,
          watchlist,
          fanslist
        })
      }
    })
  },


  // 移除粉丝
  headleRemove() {
    let that = this
    let {
      fanslist,
      removeIndex,
      removeId,
      listId
    } = that.data
    myRequesy({
      url: 'fans/delete',
      method: 'POST',
      data: {
        user_id: removeId
      }
    }).then(res => {
      if (res.data.code === 0) {
        wx.showToast({
          title: '已移除',
        })
        let obj = {
          id: fanslist[removeIndex].user.id,
          follow_state: fanslist[removeIndex].follow_state === 0 ? 0 : 1
        }
        if (listId.find(item => item.id == obj.id) !== undefined) {
          listId.splice(listId.findIndex(item1 => item1.id == obj.id), 1)
        }
        listId.push(obj)
        that.setData({
          listId
        })
        wx.setStorageSync('listId', listId)
        fanslist.splice(removeIndex, 1)
        that.setData({
          Hibom: false,
          fanspageNum: 1,
          fanslist
        })

      }
    })
  },

  // 查看主页
  navHome() {
    wx.navigateTo({
      url: '/pages/dynamic/dynamic?id=' + this.data.removeId,
    })
  },


  // 获取关注列表数据
  getAttentionList() {
    myRequesy({
      url: 'user/follows',
      data: {
        page: this.data.follpageNum
      }
    }).then(res => {
      if (res.data.code == 0) {
        this.setData({
          watchlist: res.data.data,
          nextPage: res.data.next_url
        })
      }

    })
  },

  // 获取粉丝列表数据
  getVermicelli() {
    myRequesy({
      url: 'user/fans',
      data: {
        page: this.data.fanspageNum
      }
    }).then(res => {
      if (res.data.code == 0) {
        this.setData({
          fanslist: res.data.data,
          nextPage: res.data.next_url
        })
      }
    })
  },



  //上拉加载更多关注数据
  getBomAttention() {
    let that = this
    if (that.data.nextPage === null) {
      return
    } else {
      that.data.follpageNum += 1
      myRequesy({
        url: 'user/follows',
        data: {
          page: that.data.follpageNum
        }
      }).then(res => {
        if (res.data.code == 0) {
          this.setData({
            watchlist: [...this.data.watchlist, ...res.data.data],
            nextPage: res.data.next_url
          })
        }
      })
    }
  },


  //上拉加载更多粉丝数据
  getBomVermicelli() {
    let that = this
    if (that.data.nextPage === null) {
      return
    } else {
      that.data.fanspageNum += 1
      myRequesy({
        url: 'user/fans',
        data: {
          page: that.data.fanspageNum
        }
      }).then(res => {
        if (res.data.code == 0) {
          this.setData({
            fanslist: [...this.data.fanslist, ...res.data.data],
            nextPage: res.data.next_url
          })
        }
      })
    }
  },


  //点击头像跳转到用户动态
  headleNav(e) {
    wx.navigateTo({
      url: '/pages/dynamic/dynamic?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.tablist[0].status) {
      this.getBomAttention()
    }
    this.getBomVermicelli()

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})